package Othello;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GamePanel  extends JPanel implements MouseListener, Observer{
// MouseListener is control by mouse.
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5823997689666193927L;
	static final int SIZE = 100; // size of block
	static final int WIDTH = SIZE*8; // size of width
	static final int HEIGHT = SIZE*8; // size of height

       GameStatus st = new GameStatus();    
   	   RandomCPU cpu = new RandomCPU();
   	  StatusPanel s1 = new StatusPanel(st); 
	public GamePanel(){



		this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
		this.addMouseListener(this);
        this.setLayout(null);
        this.setBounds(200, 0, 800, 800);
        s1.setBounds(800, 0, 200, 800);
		add(s1);
		st.addObserver(this);
		
	}
	public void paintComponent(Graphics a){
		
		a.setColor(Color.GREEN);
		a.fillRect(0, 0, WIDTH, HEIGHT);
		
		
		// 8*8 Line
		a.setColor(Color.BLACK);
		for (int i = 0; i<8; i++)
		{
			a.drawLine(0, i*SIZE, WIDTH, i*SIZE);
			a.drawLine(i*SIZE, 0, i*SIZE, HEIGHT);
			// 8*8 line
		}
		//a.drawRect(SIZE*2, SIZE*2, SIZE*4, SIZE*4);
        /********************************************/
		//stone
	for(int y=0; y<8; y++){
		for(int x=0; x<8; x++){
			if(st.data[x][y] == 1){
				a.setColor(Color.BLACK);  // black is 1
				a.fillOval(x*SIZE, y*SIZE, SIZE, SIZE);
			}else if(st.data[x][y] == -1){ // white is -1
				a.setColor(Color.WHITE);
				a.fillOval(x*SIZE, y*SIZE, SIZE, SIZE);
			}
		}
	}
	/*a.setColor(Color.RED);
	a.drawString("TURN = "+st.turn, 10, 30);
	a.drawString("PLAYER = "+st.player, 10, 50);
	a.drawString("DISC = "+st.black+" : " +st.white, 10, 70);	
	*/
	s1.update(st.black, st.white, st.turn);
	}
	

	
	
	
	//Mouse Move
	@Override
	public void update(Observable o, Object arg){
		repaint();
	}
	
    public void mousePressed(MouseEvent e){

		int x = e.getX();
		int y = e.getY();
		
		x /= SIZE;
		y /= SIZE;
		System.out.println("(x,y)=("+x+","+y+")");
		
		//If you cannot put on  stone, you got message.
		if( st.put(x,y) == false ){
			JOptionPane.showMessageDialog(this, "Can't put!");
		}
		
		//when finish put stone on every block.
		if(st.turn == 60){
			if (st.black > st.white)
			{
				JOptionPane.showMessageDialog(this, "Black won!!");
				//want to wait time or show button for re-game or exit.
		
			}
			else if (st.white > st.black)
			{
				JOptionPane.showMessageDialog(this, "White won!!");
				//want to wait time or show button for re-game or exit.
			}
			else if (st.black == st.white)
			{
				
				JOptionPane.showMessageDialog(this, "TIE!");
			}
                        // want to show "which player is won".
		}
		//Pass check. if player cannnot put stone on.
		else if( st.checkPass() == true ){
			st.player *= -1;
			JOptionPane.showMessageDialog(this, "Pass! Next "+st.player);
		}
		
		//CPU or 2nd player
		if(st.player == cpu.color){
			
			int action[] = cpu.decide(st);
			if(action[0] != -1)
				st.put(action[0], action[1]);
			
			//when finish put stone on every block.
			if(st.turn == 60){

			}
			//check
			else if( st.checkPass() == true ){
				JOptionPane.showMessageDialog(this, "Pass! Next turn is "+st.turn);
			}
		}
	}
	
	
    @Override
	public void mouseClicked(MouseEvent e){

	}
    @Override
	public void mouseReleased(MouseEvent e){

	}
    @Override
	public void mouseEntered(MouseEvent e){

	}
    @Override
	public void mouseExited(MouseEvent e){

	}

	
	
}
